<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in rolesList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>

    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSave">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from '@/api/roles'
import { UserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employee'
export default {
  name: 'AssginRole',
  props: {
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      rolesList: []
    }
  },
  created() {
    this.laodRoleList()
    this.loadRoleDetailById()
  },
  methods: {
    closeDialog() {
      this.$emit('close')
    },
    async laodRoleList() {
      const res = await getRoles()
      // console.log(res)
      this.rolesList = res.data.rows
    },
    async loadRoleDetailById() {
      const res = await UserDetailById(this.id)
      // console.log(res)
      this.roleIds = res.data.roleIds
    },
    async hSave() {
      const res = await assignRoles({
        id: this.id,
        roleIds: this.roleIds
      })
      this.$message.success(res.message)
      this.$emit('close')
    }
  }
}
</script>
